﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using QuanCafe.DataLayer;

namespace QuanCafe.BusinessLayer
{
    public class News
    {
        public string activity;
        public int NewsID;
        public int CategoryID;
        public string Title;
	    public string Description;
	    public string  Content;
	    public string ThumbnailURL;
	    public string PostDate;
	    public string Creator;
        public int Status;
        public string Tags;
        public int isSpecialNews;

        public int GroupCategoryID;

        SQLHelper db = new SQLHelper();

        public DataTable GetAll()
        {
            try
            {
                db.AddParameter("@Activity", "GetAll", SqlDbType.NVarChar);
                return db.ExecuteReader("SP_News", CommandType.StoredProcedure);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public DataTable GetListForViewed(int iQuantity)
        {
            try
            {
                db.AddParameter("@Activity", "GetListForViewed", SqlDbType.NVarChar);
                if (iQuantity != -1) db.AddParameter("@Quantity", iQuantity, SqlDbType.Int);
                return db.ExecuteReader("SP_News", CommandType.StoredProcedure);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public DataRow GetByID()
        {
            try
            {
                db.AddParameter("@Activity", "GetByID", SqlDbType.NVarChar);
                db.AddParameter("@NewsID", NewsID, SqlDbType.Int);
                DataTable dtb = db.ExecuteReader("SP_News", CommandType.StoredProcedure);

                if (dtb.Rows.Count > 0)
                    return dtb.Rows[0];
                else
                    return null;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public string InsertUpdate()
        {
            try
            {
                db.AddParameter("@Activity", activity, SqlDbType.NVarChar);
                db.AddParameter("@NewsID", NewsID, SqlDbType.Int);
                db.AddParameter("@CategoryID", CategoryID, SqlDbType.Int);
                db.AddParameter("@Title", Title, SqlDbType.NVarChar);
                db.AddParameter("@Description", Description, SqlDbType.NVarChar);
                db.AddParameter("@Content", Content, SqlDbType.NText);
                db.AddParameter("@ThumbnailURL", ThumbnailURL, SqlDbType.NVarChar);
                db.AddParameter("@Creator", Creator, SqlDbType.NVarChar);
                db.AddParameter("@Status", Status, SqlDbType.Int);
                db.AddParameter("@SpecialNews", isSpecialNews, SqlDbType.Int);
                db.AddParameter("@Tags", Tags, SqlDbType.NVarChar);
                return db.ExecuteScalar("SP_News", CommandType.StoredProcedure).ToString();
            }
            catch
            {
                return null;
            }
        }
        public void ChangeActive()
        {
            try
            {
                db.AddParameter("@Activity", "ChangeActive", SqlDbType.NVarChar);
                db.AddParameter("@NewsID", NewsID, SqlDbType.Int);
                db.ExecuteScalar("SP_News", CommandType.StoredProcedure);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public void ChangeRank(string ChangeRankCmd)
        {
            try
            {
                db.AddParameter("@Activity", "ChangeRank", SqlDbType.NVarChar);
                db.AddParameter("@ChangeRankCmd", ChangeRankCmd.ToString().ToLower(), SqlDbType.VarChar);
                db.AddParameter("@NewsID", NewsID, SqlDbType.Int);
                db.ExecuteScalar("SP_News", CommandType.StoredProcedure);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public void UpdatePostdate()
        {
            try
            {
                db.AddParameter("@Activity", "UpdatePostdate", SqlDbType.NVarChar);
                db.AddParameter("@NewsID", NewsID, SqlDbType.Int);
                db.ExecuteScalar("SP_News", CommandType.StoredProcedure);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public DataTable LoadNewsByTag()
        {
            try
            {
                db.AddParameter("@Activity", "LoadNewsByTag", SqlDbType.NVarChar);
                db.AddParameter("@Tags", Tags, SqlDbType.NVarChar);
                DataTable dtb = db.ExecuteReader("SP_News", CommandType.StoredProcedure);
                return dtb;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public DataTable LoadRelatedNews()
        {
            try
            {
                db.AddParameter("@Activity", "LoadRelatedNews", SqlDbType.NVarChar);
                db.AddParameter("@NewsID", NewsID, SqlDbType.Int);
                DataTable dtb = db.ExecuteReader("SP_News", CommandType.StoredProcedure);
                return dtb;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
